var teamPrice, num, address, teamType, sid;
var isUseJingCoupons, isUseDongCoupons, isUseGiftCards;
function initVar(d, c, e, b, a) {
	teamPrice = d;
	num = c;
	address = e;
	teamType = b;
	sid = a
}
function initVarForSecurityPay(b, c, a) {
	isUseJingCoupons = b;
	isUseDongCoupons = c;
	isUseGiftCards = a
}
var showSecurityPayUrl = "";
function showSecurityPayBlock(doFlag, isChecked) {
	if (doFlag) {
		if (isChecked) {
			if (showSecurityPayUrl.indexOf("usedBalance=false") != -1) {
				showSecurityPayUrl = showSecurityPayUrl.replace(
						"usedBalance=false", "usedBalance=true")
			}
		} else {
			if (showSecurityPayUrl.indexOf("usedBalance=true") != -1) {
				showSecurityPayUrl = showSecurityPayUrl.replace(
						"usedBalance=true", "usedBalance=false")
			}
		}
	}
	jQuery
			.get(
					showSecurityPayUrl,
					{},
					function(data) {
						if (data != null) {
							var obj = eval("(" + data + ")");
							if (obj.showSecurityPayBlock) {
								if (data.indexOf("securityPayBlocks") != -1) {
									for ( var i = 0; i < obj.securityPayBlocks.length; i++) {
										if ($("#showSecurityPayBlockId")
												.children().is("ul")
												&& $("#showSecurityPayBlockId")
														.children().attr(
																"class") == "html5") {
											$("#showSecurityPayBlockId")
													.children().empty();
											$("#showSecurityPayBlockId")
													.children()
													.append(
															"<li class='last'><label>"
																	+ obj.securityPayBlocks[i].title
																	+ "</label>&nbsp;&nbsp;<div  style='font-size:0.9em;color:#cc0000;text-adivgn:center;margin-bottom:0.6em;margin-left:0.6em'><img src='/images/html5/help.png'>&nbsp;使用账号余额或优惠券时，输入支付密码可以保障您的资产安全</div><div><input type='password' name='order."
																	+ obj.securityPayBlocks[i].submitKey
																	+ "'  class='common-input paypwd'></div></li>");
											addPayPasswordLink()
										} else {
											if ($("#showSecurityPayBlockId")
													.children().is("ul")
													&& $(
															"#showSecurityPayBlockId")
															.children().attr(
																	"class") == "two") {
												$("#showSecurityPayBlockId")
														.children().empty();
												$("#showSecurityPayBlockId")
														.children()
														.append(
																"<li>"
																		+ obj.securityPayBlocks[i].title
																		+ "<input type='password' name='order."
																		+ obj.securityPayBlocks[i].submitKey
																		+ "' class='common-input paypwd'></li>")
											} else {
												if ($("#showSecurityPayBlockId")
														.children().is("ul")) {
													$("#showSecurityPayBlockId")
															.children().empty();
													$("#showSecurityPayBlockId")
															.children()
															.append(
																	"<li><b>"
																			+ obj.securityPayBlocks[i].title
																			+ "</b></li>");
													$("#showSecurityPayBlockId")
															.children()
															.append(
																	"<li><input type='password' name='order."
																			+ obj.securityPayBlocks[i].submitKey
																			+ "' class='common-input paypwd'></li>")
												} else {
													$("#showSecurityPayBlockId")
															.children().empty();
													$("#showSecurityPayBlockId")
															.children()
															.append(
																	"<li class='last'><label>"
																			+ obj.securityPayBlocks[i].title
																			+ "</label><p><input type='password' name='order."
																			+ obj.securityPayBlocks[i].submitKey
																			+ "'  class='common-input'></p></li>")
												}
											}
										}
									}
								}
								$("#showSecurityPayBlockId").show()
							} else {
								$("#showSecurityPayBlockId").hide();
								$("#findPayPassword").hide();
								$("#openPayPassword").hide()
							}
						}
					})
}
function processYunFeeResultList(b) {
	var a = "";
	jQuery.each(b, function(c, d) {
		if (d.label.indexOf("运费") != -1 || d.label.indexOf("优惠券") != -1
				|| d.label.indexOf("礼品卡") != -1 || d.label.indexOf("返现") != -1
				|| d.label.indexOf("余额") != -1) {
			a += d.operator + " "
		}
		a += d.label + "：";
		if (d.label.indexOf("应付总额") != -1) {
			a += "<span style='color:red;'>￥" + d.value + " 元</span><br/>"
		} else {
			a += d.value + " 元<br/>"
		}
	});
	return a
}
var citytemp;
var areatemp;
var towntemp;
function provinceData(idprovince) {
	if (typeof (idprovince) == "undefined") {
		idprovince = 1
	}
	jQuery
			.get(
					"/order/area.action?sid=" + sid,
					{
						idProvince : 0
					},
					function(data) {
						var data = eval("(" + data + ")");
						if (data != null) {
							$("#address_province").empty();
							for ( var i = 0; i < data.length; i++) {
								$("#address_province")
										.append(
												"<option "
														+ (idprovince == data[i].id ? "selected"
																: "")
														+ " id='option_add_"
														+ data[i].id
														+ "' value="
														+ data[i].id + ">"
														+ data[i].name
														+ "</option>")
							}
						}
						$("#address_province").change()
					})
}
var citychange = function cityData() {
	var idprovince = $("#address_province").val();
	var idcity = citytemp;
	jQuery.get("/order/area.action?sid=" + sid, {
		idProvince : idprovince
	}, function(data) {
		var data = eval("(" + data + ")");
		if (data != null) {
			$("#address_city").empty();
			for ( var i = 0; i < data.length; i++) {
				$("#address_city")
						.append(
								"<option "
										+ (idcity == data[i].id ? "selected"
												: "") + " id='option_add_"
										+ data[i].id + "' value=" + data[i].id
										+ ">" + data[i].name + "</option>")
			}
		}
		$("#address_city").change()
	})
};
var areachange = function areaData() {
	var idcity = $("#address_city").val();
	var idarea = areatemp;
	if (idcity == null) {
		$("#address_area").empty();
		$("#address_area").change();
		return
	}
	jQuery.get("/order/area.action?sid=" + sid, {
		idCity : idcity
	}, function(data) {
		var data = eval("(" + data + ")");
		if (data != null) {
			$("#address_area").empty();
			for ( var i = 0; i < data.length; i++) {
				$("#address_area")
						.append(
								"<option "
										+ (idarea == data[i].id ? "selected"
												: "") + " id='option_add_"
										+ data[i].id + "' value=" + data[i].id
										+ ">" + data[i].name + "</option>")
			}
		}
		$("#address_area").change()
	})
};
var townchange = function townData() {
	var idarea = $("#address_area").val();
	var idtown = towntemp;
	if (idarea == null) {
		$("#address_town").empty();
		$("#address_town").change();
		return
	}
	jQuery.get("/order/area.action?sid=" + sid, {
		idArea : idarea
	}, function(data) {
		var data = eval("(" + data + ")");
		if (!!data && data.length > 0) {
			$("#townaddress").show();
			$("#address_town").empty();
			for ( var i = 0; i < data.length; i++) {
				$("#address_town")
						.append(
								"<option "
										+ (idtown == data[i].id ? "selected"
												: "") + " id='option_add_"
										+ data[i].id + "' value=" + data[i].id
										+ ">" + data[i].name + "</option>")
			}
		} else {
			$("#address_town").empty();
			$("#townaddress").hide()
		}
		$("#address_town").change()
	})
};
function getData(c, a, f, b, i, d, g, j, h, e) {
	$("#address_name").val(a);
	$("#address_where").val(f);
	$("#address_mobile").val(b);
	$("#address_email").val(i);
	$("#address_zip").val(d);
	citytemp = j;
	areatemp = h;
	towntemp = e;
	provinceData(g)
}
var labelchange = function setLabel() {
	var e = $("#address_province").find("option:selected").text();
	var c = $("#address_city").find("option:selected").text();
	var d = $("#address_area").find("option:selected").text();
	var b = $("#address_town").find("option:selected").text();
	var a = $("#address_where").val().replace(e, "");
	a = a.replace(c, "");
	a = a.replace(d, "");
	a = a.replace(b, "");
	$("#address_where").val(a);
	if (null == $("#address_town").find("option:selected").text()) {
		$("#address_label").html(
				$("#address_province").find("option:selected").text()
						+ $("#address_city").find("option:selected").text()
						+ $("#address_area").find("option:selected").text())
	} else {
		$("#address_label").html(
				$("#address_province").find("option:selected").text()
						+ $("#address_city").find("option:selected").text()
						+ $("#address_area").find("option:selected").text()
						+ $("#address_town").find("option:selected").text())
	}
	$("#errorMessage1").html("")
};
$("#submit_success_button").click(function() {
});
$("#submit_back_button").click(function() {
});
$(document)
		.ready(
				function() {
					if ($("input")) {
						$("input").attr("autocomplete", "off")
					}
					$("#tuan_order_totalPrice").text("￥" + teamPrice * num);
					$("#tuan_order_num").change(
							function() {
								if (validateTuanOrderNum()) {
									var totalPrice = teamPrice
											* parseInt($("#tuan_order_num")
													.val());
									totalPrice = totalPrice.toFixed(2);
									$("#tuan_order_totalPrice").text(
											"￥" + totalPrice)
								}
							});
					var validateTuanOrderNum = function() {
						$("#tuan_order_num_error").text("");
						var num = $("#tuan_order_num").val();
						if (num == null || num == "") {
							$("#tuan_order_num_error").text("数量不能为空");
							return false
						} else {
							if (!/^(\d)+$/.exec(num)) {
								$("#tuan_order_num_error").text("非法数量");
								return false
							} else {
								return true
							}
						}
					};
					var validateTuanOrderMobile = function() {
						$("#tuan_order_mobile_error").text("");
						var mobile = $("#tuan_order_mobile").val();
						if (mobile == null || mobile == "") {
							$("#tuan_order_mobile_error").text("手机号码不能为空");
							return false
						} else {
							if (!/^1(\d){10}$/.exec(mobile)) {
								$("#tuan_order_mobile_error").text("请输入正确的手机号");
								return false
							} else {
								return true
							}
						}
					};
					var checkTuanOrderAddress = function() {
						if (teamType == "lottery" || teamType == "normal") {
							return true
						}
						if (address == "") {
							window.location.href = "#address";
							return false
						}
						return true
					};
					$("#order_form").submit(
							function() {
								if (checkTuanOrderAddress()
										&& validateTuanOrderMobile()
										&& validateTuanOrderNum()) {
									$("#tuan_order_submit").attr("disabled",
											"disabled");
									return true
								}
								return false
							});
					$("#tuan_order_mobile").change(validateTuanOrderMobile);
					$("#tuan_order_num").change(validateTuanOrderNum);
					var oldIsUseBalance = "undefined";
					var usebalanceflag = document
							.getElementById("isUseBalance");
					if (!!usebalanceflag && usebalanceflag.checked) {
						oldIsUseBalance = "checked"
					}
					var calcYunfee = function() {
						if (validateNum()) {
							var num = $("#order_tuan_num").val();
							var orderYunFeeOperate = $("#orderYunFeeOperate")
									.val();
							var isUseBalance = $("#useBalance").attr("checked") == "checked";
							if (orderYunFeeOperate == "html5") {
								if (oldIsUseBalance == "checked") {
									isUseBalance = false
								} else {
									isUseBalance = true
								}
							}
							jQuery
									.get(
											"/order/calcYunfee.action",
											{
												wareId : $("#order_tuan_size")
														.val(),
												sid : sid,
												quantity : num,
												useBalance : isUseBalance
											},
											function(data) {
												if (data != null) {
													$("#order_tuan_yunfeeList")
															.html("");
													if (orderYunFeeOperate == "html5") {
														var balanceobj = document
																.getElementById("isUseBalance");
														if (oldIsUseBalance == "checked") {
															balanceobj.checked = false;
															oldIsUseBalance = "undefined";
															showSecurityPayBlock(
																	true, false)
														} else {
															balanceobj.checked = true;
															oldIsUseBalance = "checked";
															showSecurityPayBlock(
																	true, true)
														}
														var strVal = processYunFeeResultList(data);
														$(
																"#order_tuan_yunfeeList")
																.html(strVal)
													} else {
														if (isUseBalance) {
															showSecurityPayBlock(
																	true, true)
														} else {
															showSecurityPayBlock(
																	true, false)
														}
														jQuery
																.each(
																		data,
																		function(
																				i,
																				item) {
																			$(
																					"#order_tuan_yunfeeList")
																					.append(
																							function() {
																								return "<li>"
																										+ item.label
																										+ " <b>"
																										+ (item.operator == null ? ""
																												: item.operator)
																										+ " "
																										+ item.value
																										+ " 元</b> </li>"
																							})
																		})
													}
												}
											}, "json")
						}
					};
					var showSecurityPayIsDo = false;
					showSecurityPayUrl = "/order/showSecurityPayBlock.action?sid="
							+ sid;
					var isUsebalance1 = document.getElementById("isUseBalance");
					var isUsebalance2 = document.getElementById("useBalance");
					if ((jQuery("#isUseBalance").length > 0 && (!!isUsebalance1 && isUsebalance1.checked))
							|| (jQuery("#useBalance").length > 0 && (isUsebalance2 && isUsebalance2.checked))) {
						showSecurityPayUrl += "&order.usedBalance=true";
						showSecurityPayIsDo = true
					} else {
						showSecurityPayUrl += "&order.usedBalance=false"
					}
					if (isUseJingCoupons == "true") {
						showSecurityPayUrl += "&order.usedJingCoupons=true";
						showSecurityPayIsDo = true
					} else {
						showSecurityPayUrl += "&order.usedJingCoupons=false"
					}
					if (isUseDongCoupons == "true") {
						showSecurityPayUrl += "&order.usedDongCoupons=true";
						showSecurityPayIsDo = true
					} else {
						showSecurityPayUrl += "&order.usedDongCoupons=false"
					}
					if (isUseGiftCards == "true") {
						showSecurityPayUrl += "&order.usedGiftCards=true";
						showSecurityPayIsDo = true
					} else {
						showSecurityPayUrl += "&order.usedGiftCards=false"
					}
					if (showSecurityPayIsDo) {
						showSecurityPayBlock(false, false)
					}
					var colorChange = function sizeData() {
						var color = $("#order_tuan_color").val();
						jQuery.get("/order/size.action?sid=" + sid, {
							wareId : color
						}, function(data) {
							var data = eval("(" + data + ")");
							if (data != null) {
								$("#order_tuan_size").empty();
								for ( var i = 0; i < data.length; i++) {
									$("#order_tuan_size").append(
											"<option id='option_add_"
													+ data[i].id + "' value="
													+ data[i].id + ">"
													+ data[i].name
													+ "</option>")
								}
							}
						})
					};
					$("#order_tuan_num").change(calcYunfee);
					$("#order_tuan_color").change(colorChange);
					$("#order_tuan_size").change(calcYunfee);
					$("#useBalance").click(calcYunfee);
					var validateNum = function() {
						$("#order_tuan_num_error").text("");
						if (!document.getElementById("order_tuan_num")) {
							return true
						}
						var num = $("#order_tuan_num").val();
						if (num == null || num == "") {
							$("#order_tuan_num_error").text("数量不能为空");
							return false
						} else {
							if (!/^(\d)+$/.exec(num)) {
								$("#order_tuan_num_error").text("请输入正确数量");
								return false
							} else {
								return true
							}
						}
					};
					var checkAddress = function() {
						if (address == "") {
							$("#errorMessage").text("请输入收货人地址");
							window.location.href = "#address";
							return false
						}
						return true
					};
					var checkNoRegisterOrder = function() {
						var loginflag = $("#loginflag").val();
						if (loginflag == "true") {
							return true
						}
						var registermobile = $("#mobile").val();
						var registerpwd = $("#password").val();
						if (loginflag == "false") {
							if (registermobile == "" || registerpwd == "") {
								$("#nameNull").show();
								$("#nameNull").text("请输入电话号码或密码");
								return false
							}
						}
						return true
					};
					var checkSecurityPayPassword = function() {
						var securityPayPassword = $(
								"input[name='order.securityPayPassword']")
								.val();
						if (securityPayPassword.trim() == ""
								&& $("#showSecurityPayBlockId").attr("style")
										.indexOf("none") == -1) {
							alert("支付密码不能为空");
							$("input[name='order.securityPayPassword']")
									.focus();
							return false
						}
						return true
					};
					var checkRemarkStringLength = function() {
						var remark = $("input[name='order.remark']").val();
						if (remark != null && remark.trim() != ""
								&& remark.length > 15) {
							alert("订单备注限制在15个字以内");
							$("input[name='order.remark']").focus();
							return false
						}
						return true
					};
					var orderDate = new Array();
					$("#order_tuan_form")
							.submit(
									function() {
										orderDate.push(new Date());
										if (orderDate.length > 1
												&& (orderDate[orderDate.length - 1]
														.getTime()
														- orderDate[orderDate.length - 2]
																.getTime() < 5000)) {
											return false
										}
										if (checkAddress()
												&& checkNoRegisterOrder()
												&& validateNum()
												&& checkSecurityPayPassword()
												&& checkRemarkStringLength()) {
											return true
										}
										return false
									});
					$("#order_tuan_num").change(validateNum);
					var validateName = function() {
						var name = $("#address_name").val();
						if (name == null || name.trim() == "") {
							$("#name_error").text("收货人不能为空");
							return false
						} else {
							$("#name_error").text("");
							return true
						}
					};
					$("#address_name").blur(validateName);
					var validateWhere = function() {
						var where = $("#address_where").val();
						if (where == null || where.trim() == "") {
							$("#where_error").text("地址不能为空");
							return false
						} else {
							$("#where_error").text("");
							return true
						}
					};
					$("#address_where").blur(validateWhere);
					var validateAddressMobile = function() {
						var mobile = $("#address_mobile").val();
						if (mobile == null || mobile.trim() == "") {
							$("#mobile_error").text("手机号不能为空");
							return false
						} else {
							if (!/^1(\d){10}$/.exec(mobile)) {
								$("#mobile_error").text("请输入正确的手机号");
								return false
							} else {
								$("#mobile_error").text("");
								return true
							}
						}
					};
					$("#address_mobile").blur(validateAddressMobile);
					var validateSubmit = function() {
						var resultVal = true;
						var initSign = $("#address_label").html();
						if (initSign == null || initSign == "") {
							$("#errorMessage1").html("地址加载中...");
							resultVal = false
						}
						if (!validateWhere()) {
							resultVal = false
						}
						if (!validateName()) {
							resultVal = false
						}
						if (!validateAddressMobile()) {
							resultVal = false
						}
						if (resultVal) {
							return true
						} else {
							return false
						}
					};
					$("#addressForm").submit(validateSubmit);
					$("#address_submit").click(validateSubmit);
					$("#address_province").change(citychange);
					$("#address_city").change(areachange);
					$("#address_area").change(townchange);
					$("#address_town").change(labelchange);
					$("#back").click(function() {
						return history.go(-1)
					});
					$("#btnTip").click(function() {
						$(".pop-prompt").hide()
					})
				});
function showpayPasswordTip() {
	$(".pop-prompt").show()
}
function addPayPasswordLink() {
	jQuery.get("/user/payPasswordLink.json?", {}, function(a) {
		if (a.isSuccess == "T") {
			$("#findPayPassword").show()
		} else {
			$("#openPayPassword").show()
		}
	}, "json")
};